package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: MenuService
 * @Author: LiuJunMeng
 * @Date: 2020/2/17 11:47
 * @Description: //TODO
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        //使用分页查询助手进行查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //进行字符串拼接
        String queryString = queryPageBean.getQueryString();
        if (StringUtil.isNotEmpty(queryString)) {
            queryString = "%" + queryString + "%";
        }
        Page<Menu> page = menuDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @param parentMenuIds
     */
    @Override
    public void add(Menu menu, Integer[] parentMenuIds) {
        if (menu.getLevel() == 1) {
            //查最大序号
            Integer mun = null;
            Integer p = menuDao.findMaxpriority();
            menu.setPriority(p + 1);
            menu.setPath(p + 2 + "");
        } else {
            menu.setParentMenuId(parentMenuIds[0]);
            //查父菜单路径
            String path = menuDao.findparentpath(parentMenuIds[0]);
            //查最大序号
            Integer priority1 = menuDao.findMaxpriorityByparentMenuId(parentMenuIds[0]);
            menu.setPriority(priority1 + 1);
            menu.setPath("/" + path + "-" + priority1 + 1);
        }
        menuDao.add(menu);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    /**
     * 根据id删除菜单
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        menuDao.deleteById(id);
    }

    /**
     * 编辑检查组
     *
     * @param menu
     * @param parentMenuIds
     */
    @Override
    public void edit(Menu menu, Integer[] parentMenuIds) {
        if (menu.getLevel() == 1) {
            //查最大序号
            Integer mun = null;
            Integer p = menuDao.findMaxpriority();
            menu.setPriority(p + 1);
            menu.setPath(p + 2 + "");
        } else {
            menu.setParentMenuId(parentMenuIds[0]);
            //查父菜单路径
            String path = menuDao.findparentpath(parentMenuIds[0]);
            //查最大序号
            Integer priority1 = menuDao.findMaxpriorityByparentMenuId(parentMenuIds[0]);
            System.out.println(priority1);
            menu.setPriority(priority1 + 1);
            menu.setPath("/" + path + "-" + priority1 + 1);
        }
        menuDao.edit(menu);
    }

    /**
     * 根据id查父菜单
     *
     * @param id
     * @return
     */
    @Override
    public Integer[] findParentMenuIDById(Integer id) {
        return menuDao.findParentMenuIDById(id);
    }

    /**
     * 根据ID查询菜单
     *
     * @param id
     * @return
     */
    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }
}
